package com.qf.collection;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class MyCollection implements Collection {

    private Object[] arr = new Object[1000];
    int count = 0;

    @Override
    public int size() {
        return count;
    }

    @Override
    public boolean isEmpty() {
        if (count == 0) return true;

        return false;
    }

    @Override
    public boolean contains(Object o) {
        for (int i = 0; i < count; i++) {
            if (arr[i].equals(o)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Iterator iterator() {
        return new Iterator() {

            int index = 0;

            @Override
            public boolean hasNext() {
                return index < count;
            }

            @Override
            public Object next() {
                return arr[index++];
            }
        };
//        return new MyCollectionIterator(arr,count);
    }

    @Override
    public Object[] toArray() {
        Object[] objects = new Object[arr.length];
        for (int i = 0; i < objects.length; i++) {
            objects[i] = arr[i];
        }
        return objects;
    }

    @Override
    public boolean add(Object o) {
        if (count <= arr.length) {
            arr[count] = o;
            count++;
            return true;
        }

        return false;

    }

    @Override
    public boolean remove(Object o) {

        int i = 0;
        for (; i < arr.length; i++) {
            if (arr[i].equals(o)) {
                arr[i] = null;
            }
        }

        for (int j = i + 1; j < arr.length; j++) {
            arr[j] = arr[j - 1];
        }

        if (contains(o) == false) return true;

        return false;
    }

    @Override
    public boolean addAll(Collection c) {
        for (Object o : c) {
            if (add(o) == false) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void clear() {
        Object[] objects = new Object[arr.length];
        arr = objects;
        count = 0;
    }

    @Override
    public boolean retainAll(Collection c) {
        Object[] newArr = new Object[arr.length];
        int count = 0;
        for (Object o : arr) {
            for (Object o1 : c) {
                if (o1.equals(o)) {
                    newArr[count] = o;
                    count++;
                }

            }
        }
        if (newArr == null) return false;
        return true;
    }

    @Override
    public boolean removeAll(Collection c) {
        for (Object o : c) {
            remove(o);
        }

        for (Object o : c) {
            if (contains(o)) {
                return false;
            }
        }

        return true;
    }

    @Override
    public boolean containsAll(Collection c) {
        for (Object o : c) {
            if (contains(o) == false) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Object[] toArray(Object[] a) {
        Object[] newObj = new Object[arr.length];
        int count = 0;
        for (Object o : arr) {
            newObj[count] = o;
            count++;
        }
        return new Object[0];
    }
}
